#include <QtGui>
#include "QtInstrument.h"



QtInstrument::QtInstrument(QtStraStrategyInfo* barinfo, BarSeries* pBarSer, BarData* pData, QWidget *parent): 
QSplitter(Qt::Vertical, parent)
{
	
	CreateBusicoord(pBarSer, pData);

	CreateMainbar(barinfo);

	CreateContextMenu();

	

	//setBackgroundRole(QPalette::Dark);
	//setAutoFillBackground(true);
	//setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	setFocusPolicy(Qt::StrongFocus);


}

QtInstrument::~QtInstrument()
{
	//m_pBusicoord couldn't be deleted by widget. So delete it manually
	DestroyBusicoord();

	
	//mainbar action, m_pfrombar
	//should be destroyed by this widget self. So needn't delete them manually
}


void QtInstrument::MdUpdate(BarData* pData)
{
	if (m_pBusiCoord->SetBarData(pData))
	{

		if (m_pmainbar && m_pmainbar->isVisible())
			m_pmainbar->RefreshBuffer();

		if (m_pform_bar && m_pform_bar->isVisible())
			m_pform_bar->RefreshBuffer();


	}
	



}


//Scroll won't cause not shown bar like resizeORzoom
//
void QtInstrument::Scroll(int dx)
{
	if (m_pBusiCoord->SetScroll(dx))
	{

		if (m_pmainbar && m_pmainbar->isVisible())
			m_pmainbar->RefreshBuffer();

		if (m_pform_bar && m_pform_bar->isVisible())
			m_pform_bar->RefreshBuffer();
	}

}



void QtInstrument::Zoom(zoomset option)
{
	if (m_pBusiCoord->SetZoom(option))
	{
		if (m_pmainbar && m_pmainbar->isVisible())
			m_pmainbar->RefreshBuffer();

		if (m_pform_bar && m_pform_bar->isVisible())
			m_pform_bar->RefreshBuffer();
	}

}





void QtInstrument::resizeEvent(QResizeEvent *  event )
{

	pantheios::log_WARNING("[resizeEvent] width:", panint(width()), " height:", panint(height()));


	m_pBusiCoord->SetBusiRect(QRect(0, 0, width(), height()));


	QSplitter::resizeEvent(event);
}



void QtInstrument::mousePressEvent(QMouseEvent *event)
{

	if (event->button() == Qt::LeftButton && m_pBusiCoord->IsValidCurosr(QPoint(event->x(), event->y()))) 
	{

		printf("[QtInstrument::mousePressEvent] x:%d, y:%d\n", event->x(), event->y());


		if (m_pmainbar && m_pmainbar->isVisible())
			m_pmainbar->MousePress(QPoint(event->x(), event->y()));

		if (m_pform_bar && m_pform_bar->isVisible())
			m_pform_bar->MousePress(QPoint(event->x(), event->y()));




		setCursor(Qt::CrossCursor);
	}
}



void QtInstrument::mouseMoveEvent(QMouseEvent *event)
{

	if (m_pBusiCoord->IsValidCurosr(QPoint(event->x(), event->y()))) 
	{
		printf("[QtInstrument::mouseMoveEvent] x:%d, y:%d\n", event->x(), event->y());



		if (m_pmainbar && m_pmainbar->isVisible())
			m_pmainbar->MouseMove(QPoint(event->x(), event->y()));

		if (m_pform_bar && m_pform_bar->isVisible())
			m_pform_bar->MouseMove(QPoint(event->x(), event->y()));


	}



}



void QtInstrument::mouseReleaseEvent(QMouseEvent *event)
{
	if ((event->button() == Qt::LeftButton)) 
	{
		printf("[QtInstrument::mouseReleaseEvent] x:%d, y:%d\n", event->x(), event->y());


		m_pBusiCoord->SetCursorORUncursor(false);

		if (m_pmainbar && m_pmainbar->isVisible())
			m_pmainbar->MouseRelease(QPoint(event->x(), event->y()));

		if (m_pform_bar && m_pform_bar->isVisible())
			m_pform_bar->MouseRelease(QPoint(event->x(), event->y()));




		unsetCursor();
	}


}

void QtInstrument::keyPressEvent(QKeyEvent *event)
{

	switch (event->key()) 
	{
	case Qt::Key_Left:

		Scroll(-1);
		break;

	case Qt::Key_Right:

		Scroll(1);
		break;

	case Qt::Key_Down:

		Zoom(zoomout);
		break;

	case Qt::Key_Up:

		Zoom(zoomin);
		break;

	}
}



QSize QtInstrument::sizeHint() const
{
	return QSize(12 * BusiCoord::LeftMargin, 8 * BusiCoord::LeftMargin);
}




void QtInstrument::CreateContextMenu()
{
	m_pform_bar = NULL;
	m_pformbar_action = new QAction(tr("K"), this);
	m_pformbar_action->setStatusTip(tr("Add Indicator K"));
	m_pformbar_action->setCheckable(true);
	m_pformbar_action->setChecked(false);
	connect(m_pformbar_action, SIGNAL(triggered()), this, SLOT(OnContextMenu_Bar()));
	addAction(m_pformbar_action);



	this->setContextMenuPolicy(Qt::ActionsContextMenu);
}


void QtInstrument::OnContextMenu_Bar()
{
	if (m_pformbar_action->isChecked())
	{
		if (NULL == m_pform_bar)
		{
			m_pform_bar = new QtFormulaBar(m_pBusiCoord);//without barinfo

			addWidget(m_pform_bar);
		}
		else
		{
			m_pform_bar->show();
		}

	} 
	else
	{
		m_pform_bar->hide();

	}


}


void QtInstrument::CreateMainbar(QtStraStrategyInfo* barinfo)
{
	
	//init pbar formula
	m_pmainbar = new QtFormulaBar(m_pBusiCoord, barinfo);


	addWidget(m_pmainbar);


	//setStretchFactor(0, 1);

	
}




//
void QtInstrument::CreateBusicoord(BarSeries* pBarSer, BarData* pData)
{
	

	pantheios::log_WARNING("[CreateBusicoord] width:", panint(width()), " height:", panint(height()));


	m_pBusiCoord = new BusiCoord(QRect(0, 0, width(), height()), pBarSer, pData);


}

//
void QtInstrument::DestroyBusicoord()
{


	//pantheios::log_WARNING("[CreateBusicoord] width:", panint(width()), " height:", panint(height()));


	if (m_pBusiCoord)
		delete m_pBusiCoord;
	


}